/* Includes ------------------------------------------------------------------*/
#include "vaacum.h"
#include "puresun_func.h"
#include "math.h"
#include <stdio.h>
#include "main.h"
#include "485_slave.h"

/* Private macro -------------------------------------------------------------*/

/* Private typedef -----------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/

/* Private functions ---------------------------------------------------------*/

/*******************************************************************************
 * Function Name : vaacum_init
 * Description   : vaacum init
 * Input         : [vaacum_t *vaacum, int baud, byte adr]
 *                 - vaacum: vaacum struct
 *                 - adr: vaacum address
 * Output        : None
 * Return        : None
 * Notes         : None
 * Attention	 : None
 *******************************************************************************/
void vaacum_init(vaacum_t *vaacum, byte adr)
{
    // check vaacum
    if (vaacum == NULL)
        return;

    // check address
    if (adr < 0x01 || adr > 0x7F)
        return;

    // init vaacum
    vaacum->adr = adr;            // device address
    vaacum->value = 100 * 1000.0; // 100kPa
    vaacum->digital = 10;         // 1.0
    vaacum->pow = 5;              // E5
}

/*******************************************************************************
 * Function Name : vaacum_analyze
 * Description   : analyze vaacum data
 * Input         : [vaacum_t *vaacum, byte *frame_in, uint16_t datalen]
 *                 - vaacum: vaacum struct
 * Output        : None
 * Return        : None
 * Notes         : None
 * Attention	 : None
 *******************************************************************************/
void vaacum_analyze(vaacum_t *vaacum)
{
    // check vaacum
    if (vaacum == NULL)
        return;

    // check command
    byte buf[4] = {'1', '0', '+', '5'};
    // md_get_value_from_mem(&device.modbus, vaacum->adr, 0x006B, (modbus_reg_value_t *)buf);
    // md_get_value_from_mem(&device.modbus, vaacum->adr, 0x006C, (modbus_reg_value_t *)(buf + 2));
    // printf("vaacum_analyze buf: [%c,%c,%c,%c]\n", buf[0], buf[1], buf[2], buf[3]);

    // the value in the register is like 10+5, which means 1.0E5
    // 10-1 which means 1.0E-1

    double value = (buf[1] - '0');
    value += (double)(buf[0] - '0') * 0.1;
    int powx = buf[2] - '0';
    if (buf[3] == '-')
    {
        powx = -powx;
    }

    // check value and pow
    if (value > 0 && value < 10 && powx > -10 && powx < 10)
    {
        vaacum->value = value * pow(10, powx);
        vaacum->pow = powx;
        vaacum->digital = (int8_t)(value * 10);
    }
}
/************************ (C) COPYRIGHT PURESUN **** END OF FILE ***************/
